package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserSkin;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/9/1
 */
public class UserSkinDaoImpl extends TempDataObjectDao<UserSkin> {
    public static String getUserSkinSql = "select * from t_u_skin where userId=?";

    public Map<Integer, UserSkin> getUserSkinMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserSkin> userSkinMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserSkinSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserSkin userSkin = parse(rs);
                userSkinMap.put(userSkin.getSkinId(), userSkin);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userSkinMap;
    }

    public static String updateUserSkinSql = "update t_u_skin set level=? where userId=? and skinId=?";

    @Override
    protected boolean updateData(UserSkin userSkin) {
        userSkin.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userSkin.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userSkin.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userSkin.getSkinId()));
        if (getDb().executeNoneQuery(updateUserSkinSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserSkinSql = "insert into t_u_skin(userId,skinId,level) values (?,?,?)";

    @Override
    protected boolean addData(UserSkin userSkin) {
        userSkin.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userSkin.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userSkin.getSkinId()));
        para.put(i++, new DbParameter(Types.INTEGER, userSkin.getLevel()));
        if (getDb().executeNoneQuery(insertUserSkinSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserSkin parseData(ResultSet rs) throws SQLException {
        UserSkin userSkin = new UserSkin();
        userSkin.setUserId(rs.getLong("userId"));
        userSkin.setSkinId(rs.getInt("skinId"));
        userSkin.setLevel(rs.getInt("level"));

        return userSkin;
    }
}
